var app = getApp();
import util from '../../utils/util.js';
const FN = require('../../utils/public');
import commonAPI from '../../api/api'
var app = getApp();
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stuWXinfo: {},
    loadingEnd: true,
    appHeight: '',
    homeworkArrTmp: [],
    historyArr: [],
    dateArr: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.initData();
  },

  initData: function () {
    var that = this;
    that.todayHomework(page, "1");
  },

  // 获取今日作业
  todayHomework: function (pi, freshType) {
    let that = this, params = { pageNum: pi, pageSize: 10, studyTaskState: '1' };
    commonAPI.repeatHistoryPush(params).then(res => {
      var resArr = that.data.homeworkArrTmp;
      setTimeout(() => {
        wx.hideNavigationBarLoading(); // 完成停止加载图标
        wx.stopPullDownRefresh(); // 停止下拉
        
        if (res.data.list.length == 0) {
          FN.Toast("暂时还没学习任务哦");
          that.setData({
            loadingEnd: false
          })
        } else {
          if (freshType == "1") {
            resArr = res.data.list; // 下拉刷新
          } else if (freshType == "2") {
            resArr.push.apply(that.data.homeworkArrTmp, res.data.list); //上拉加载
          }
          that.dealData(res);
        }
      }, 500);
    })
  },

  // 处理数据
  dealData: function (res) {
    var resArr = res.data.list,
      that = this;
    setTimeout(() => {
      let dateArr = null;
      if (resArr[0] && resArr[0].date) {
        dateArr = util.formatTimeTool(new Date(Date.parse(resArr[0].date.replace('年', '-').replace('月', '-').replace('日', ''))));
      }
      resArr.map(item=>{
        item.homeworkDate = util.formatTimeTool(new Date(item.date)).join("-")
      })
      that.setData({
        historyArr: resArr,
        loadingEnd: false,
        dateArr: dateArr
      });
    }, 500)
  },

  // 查看书籍
  aboutBook: function (e) {
    var booksId = e.currentTarget.dataset.booksid;
    wx.navigateTo({
      url: '../myBooksChapter/myBooksChapter?booksId=' + booksId
    })
  },

  // 首页统一入口，没有班级学生和游客区分
  voiceRepeat: function (e) {
    var that = this;
    var voiceObj = that.data.historyArr[e.currentTarget.dataset.hwdindex].date2Data[e.currentTarget.dataset.voiceindex];

    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { scene: voiceObj.voiceSystemId })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    page = 1;
    that.todayHomework(page, "1");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    page++;
    this.todayHomework(page, "2");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
